package com.wencai.daily;

import com.wencai.daily.bean.DailyData;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author wangpan
 * @date 2023/6/8
 */
public class StockChooser {
    /**
     * 根据五日均量的结果从股票列表中选出对应的股票
     *
     * @param dailyDataList
     * @param amountRate
     * @return
     */
    public static DailyData chooseYesterdayAmount(List<DailyData> dailyDataList, double amountRate) {
        Optional<DailyData> first = dailyDataList.stream().filter(item -> item.getYesterdayAmount() != 0).findFirst();
        // 如果没有10日数据, 则直接返回
        if (!first.isPresent()) {
            return null;
        }
        if (dailyDataList.stream().mapToDouble(item -> item.getAmount()).max().getAsDouble() <= 0) {
            return null;
        }
        List<DailyData> collect = dailyDataList.stream().filter(item -> item.getIsSt() != 1).collect(Collectors.toList());
        if (collect.isEmpty()) {
            return null;
        }
        DailyData chose = collect.get(0);
        double gap = Math.abs(collect.get(0).getYesterdayAmountRate() - amountRate);
        for (DailyData dailyData : collect) {
            double tempGap = Math.abs(dailyData.getYesterdayAmountRate() - amountRate);
            if (tempGap < gap) {
                chose = dailyData;
                gap = tempGap;
            }
        }
        return chose;
    }

    /**
     * 根据五日均量的结果从股票列表中选出对应的股票
     *
     * @param dailyDataList
     * @param amountRate
     * @return
     */
    public static DailyData chooseFiveDayStock(List<DailyData> dailyDataList, double amountRate) {
        Optional<DailyData> first = dailyDataList.stream().filter(item -> item.getTenDayAmountRate() != 0).findFirst();
        // 如果没有10日数据, 则直接返回
        if (!first.isPresent()) {
            return null;
        }
        if (dailyDataList.stream().mapToDouble(item -> item.getAmount()).max().getAsDouble() <= 0) {
            return null;
        }
        List<DailyData> collect = dailyDataList.stream().filter(item -> item.getIsSt() != 1).collect(Collectors.toList());
        if (collect.isEmpty()) {
            return null;
        }
        DailyData chose = collect.get(0);
        double gap = Math.abs(collect.get(0).getFiveDayAmountRate() - amountRate);
        for (DailyData dailyData : collect) {
            double tempGap = Math.abs(dailyData.getFiveDayAmountRate() - amountRate);
            if (tempGap < gap) {
                chose = dailyData;
                gap = tempGap;
            }
        }
        return chose;
    }

    /**
     * 选取倍量且涨幅比昨天翻倍的票
     *
     * @param dailyDataList
     * @param amountRate
     * @return
     */
    public static DailyData chooseDoubleAmountAndDoubleIncrement(List<DailyData> dailyDataList, double amountRate) {
        Optional<DailyData> first = dailyDataList.stream().filter(item -> item.getTenDayAmountRate() != 0).findFirst();
        // 如果没有10日数据, 则直接返回
        if (!first.isPresent()) {
            return null;
        }
        if (dailyDataList.stream().mapToDouble(item -> item.getAmount()).max().getAsDouble() <= 0) {
            return null;
        }
        List<DailyData> collect = dailyDataList.stream().filter(item -> item.getIsSt() != 1)
                .collect(Collectors.toList());
        collect = collect.stream().filter(item -> {
            if (item.getYesterdayPctChg() > -1.5) {
                return false;
            }
            if (item.getPctChg() > 7) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());
        collect = collect.stream().filter(item -> {
            if (Math.abs(item.getYesterdayPctChg()) * 2 > item.getPctChg()) {
                return false;
            }
            if (item.getYesterdayAmountRate() < 2) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());


        collect = collect.stream().filter(item -> {
            if (item.getPreClose() < item.getOpen()) {
                return false;
            }
            if (item.getYesterdayAmount() > item.getFiveDayAvgAmount() && item.getYesterdayAmount() > item.getTenDayAvgAmount()) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());


        if (collect.isEmpty()) {
            return null;
        }
        DailyData chose = collect.get(0);
        double gap = Math.abs(collect.get(0).getYesterdayAmountRate());
        for (DailyData dailyData : collect) {
            double tempGap = Math.abs(dailyData.getYesterdayAmountRate());
            if (tempGap < gap) {
                chose = dailyData;
                gap = tempGap;
            }
        }
        return chose;
    }


    /**
     * 选取最低价小于等于10日线; 收盘价大于等于10日线; 缩量阴线; 10日内有涨停;
     *  缩量回踩效果不好
     * @param dailyDataList
     * @param amountRate
     * @return
     */
    public static DailyData chooseDownToTenDayAvg(List<DailyData> dailyDataList, double amountRate) {
        Optional<DailyData> first = dailyDataList.stream().filter(item -> item.getTenDayAmountRate() != 0).findFirst();
        // 如果没有10日数据, 则直接返回
        if (!first.isPresent()) {
            return null;
        }
        if (dailyDataList.stream().mapToDouble(item -> item.getAmount()).max().getAsDouble() <= 0) {
            return null;
        }
        List<DailyData> collect = dailyDataList.stream().filter(item -> item.getIsSt() != 1)
                .collect(Collectors.toList());
        collect = collect.stream().filter(item -> {
            if (item.getOpen() < item.getClose()) {
                return false;
            }

            if ((item.getOpen() - item.getClose()) * 100 / item.getOpen() < amountRate) {
//                System.out.println((item.getOpen() - item.getClose()) * 100 / item.getOpen());
                return false;
            }
            if (item.getClose() > item.getPreClose()) {
                return false;
            }
            if (item.getYesterdayAmountRate() > 1) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());

        collect = collect.stream().filter(item -> {
            if (item.getAmount() > item.getFiveDayAvgAmount() && item.getAmount() > item.getTenDayAvgAmount()) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());

        collect = collect.stream().filter(item -> {
            if (item.getTenDayMaxPctChg() > 9) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());


        collect = collect.stream().filter(item -> {
            if (item.getLow() <= item.getTenDayAvgPrice() && item.getClose() >= item.getTenDayAvgPrice() && item.getFiveDayAvgPrice() > item.getTenDayAvgPrice()
                    && item.getClose() <= item.getFiveDayAvgPrice()
            ) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());


        if (collect.isEmpty()) {
            return null;
        }
        DailyData chose = collect.get(0);
        double gap = Math.abs(collect.get(0).getFiveDayAmountRate());
        for (DailyData dailyData : collect) {
            double tempGap = Math.abs(dailyData.getFiveDayAmountRate());
            if (tempGap < gap) {
                chose = dailyData;
                gap = tempGap;
            }
        }
        return chose;
    }

    /**
     * 选取最低价小于等于10日线; 收盘价大于等于10日线; 缩量阴线; 10日内有涨停;
     *  放量突破策略
     * @param dailyDataList
     * @param amountRate
     * @return
     */

}
